package com.yao.ui;

import android.app.Activity;
import android.arch.lifecycle.LifecycleOwner;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import com.base.mvp.BaseMvpActivity;
import com.yao.BuildConfig;
import com.yao.R;
import com.yao.helper.DialogFragmentHelper;
import com.yao.presenter.UserContract;
import com.yao.presenter.UserPresenter;
import com.yao.ui.psd.PsdGestureAcitivity;
import com.yao.util.AlertUtil;
import com.yao.util.PreferenceCache;

import static com.yao.widget.PsdGestureView.STATE_REGISTER;

//@Route(path = AppConfig.Router.ACTIVITY_URL_LOGIN, group = "yao")
public class LoginActivity extends BaseMvpActivity<UserPresenter> implements UserContract.View, View.OnClickListener {

    private EditText etName, etPsd;
    private CheckBox cbPsd, cbAutoLogin;
    private Button btLogin;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!TextUtils.isEmpty(PreferenceCache.getToken())) {
            PsdGestureAcitivity.startActivity(this);
            finish();
            return;
        }
        if (PreferenceCache.isAutoLogin()) {
            mPresenter.login(PreferenceCache.getUserName(), PreferenceCache.getUserPsd());
        }
        if (BuildConfig.DEBUG) {
            etName.setText("tangpinghai");
            etPsd.setText("tangpinghai123");
        } else {
            etName.setText(PreferenceCache.getUserName());
            etPsd.setText(PreferenceCache.getUserPsd());
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected boolean useDynamicTitleBar() {
        return false;
    }

    @Override
    protected void initView(Bundle bundle) {
        mPresenter.attachView(this);
        getDecorView().setBackgroundColor(Color.parseColor("#03A9F4"));

        etName = findViewById(R.id.et_name);
        etPsd = findViewById(R.id.et_psd);

        cbPsd = findViewById(R.id.cb_psd);
        cbAutoLogin = findViewById(R.id.cb_autologin);
        cbAutoLogin.setChecked(PreferenceCache.isAutoLogin());

        cbAutoLogin.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                PreferenceCache.putAutoLogin(true);
            } else {
                PreferenceCache.putAutoLogin(false);
            }
        });

        btLogin = findViewById(R.id.bt_login);
        btLogin.setOnClickListener(this);
    }

    @Override
    protected boolean isTranslucentStatus() {
        return super.isTranslucentStatus();
    }

    @Override
    protected void release() {

    }

    @Override
    public UserPresenter bindViewToPresenter() {
        return new UserPresenter();
    }

    @Override
    public void onSuccess() {
        if (cbPsd.isChecked()) {
            PreferenceCache.putUserPsd(etPsd.getText().toString());
        } else {
            PreferenceCache.putUserPsd("");
        }
        SharedPreferences mSharedPreference = mContext.getSharedPreferences("STATE_DATA", Activity.MODE_PRIVATE);
        int state = mSharedPreference.getInt("state", STATE_REGISTER);
        if (state == STATE_REGISTER) {
            PsdGestureAcitivity.startActivity(this);
            finish();
        } else {
            IndexActivity.startActivity(this);
            finish();
        }
    }

    @Override
    public void onFail(int code, String msg) {
        AlertUtil.t(this, msg);
    }

    @Override
    public LifecycleOwner bindLifecycleOwner() {
        return this;
    }

    @Override
    public void showLoading() {
        DialogFragmentHelper.showLoadingDialog(getSupportFragmentManager());
    }

    @Override
    public void hideLoading() {
        DialogFragmentHelper.closeLoadingDialog(getSupportFragmentManager());
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bt_login) {
            String name = etName.getText().toString();
            if (TextUtils.isEmpty(name)) {
                AlertUtil.t(this, "请输入名字");
                return;
            }

            String psd = etPsd.getText().toString();
            if (TextUtils.isEmpty(psd)) {
                AlertUtil.t(this, "请输入密码");
                return;
            }

            mPresenter.login(name, psd);
        }
    }
}
